/**
 * 全局异常捕获过滤器
 */
import {
  ExceptionFilter,
  Catch,
  ArgumentsHost,
  HttpException,
} from '@nestjs/common';
import { Response } from 'express';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const status = exception.getStatus();

    response.status(status).json({
      statusCode: status,
      error: exception.message,
      msg:
        process.env.NODE_ENV === 'dev'
          ? exception
          : `${status >= 500 ? 'Service Error' : 'Client Error'}`,
    });
  }
}
